# Maintainer: gym603 <gui_yuan_miao@163.com>

_realname=julia
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.1.1
pkgrel=1
pkgdesc="Julia is a high-level, high-performance dynamic language for technical computing (mingw-w64)"
arch=('any')
url="https://julialang.org/"
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-suitesparse"
         "${MINGW_PACKAGE_PREFIX}-libgit2"
         "${MINGW_PACKAGE_PREFIX}-lapack")
makedepends=("patch"
             "make"
             "tar"
             "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"
             "${MINGW_PACKAGE_PREFIX}-diffutils"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "perl")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/JuliaLang/julia/archive/v${pkgver}.tar.gz
        0003-fix-CMAKE_GENERATOR_COMMAND-and-CMAKE_C_COMPILER.patch
        0004-fix-base-floatfuncs.jl.patch
        0007-Makefile.patch
        0008-fix-i686-gcc-dll-name.patch
        0009-find-modname-in-julia_dl-before-default-paths.patch
        0010-fix-install-dir.patch
        0011-donot-cp-LLVM.dll-when-USE_LLVM_SHLIB-disabled-on-WINNT.patch)
sha256sums=('0155a33448cb2334ae7487a35a2416a4205da4541b90fc9f97208da11e273c9e'
            'b9a82888f79ce2786a68919b37eb52f0dd44cbac53dd49a6e95401916fa4edd2'
            '221023d4ebf2721a9b5cedbc6a3550c26c3c9897f5332a6ea4a68ec1f1510b05'
            'bd05dbdfd81173d6fbecd2d35b65699d76e97ea887fab140bb7642eff31dedf6'
            '71fb4b167b9dcf5d227e8ce7e05d35301f2f6ce2b353c4215e8282d5d3a8dc33'
            'f82628d0a12c1503ce3a032c6194392f257f49c03724d6788a6d7f5789e33982'
            '0ac90c5d4d56dc65827bc5ca7189bd983f2cfb33a70ed46d48243f5db4af76df'
            '3263ef60feb20fd3605b8abd159d06e76c43c03e8aba893864182aac813cddba')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  apply_patch_with_msg \
    0003-fix-CMAKE_GENERATOR_COMMAND-and-CMAKE_C_COMPILER.patch \
    0004-fix-base-floatfuncs.jl.patch \
    0007-Makefile.patch \
    0008-fix-i686-gcc-dll-name.patch \
    0009-find-modname-in-julia_dl-before-default-paths.patch \
    0010-fix-install-dir.patch \
    0011-donot-cp-LLVM.dll-when-USE_LLVM_SHLIB-disabled-on-WINNT.patch

  #if [ ! -d .git ]; then
  #  git init
  #  git add .
  #  git commit -m "init upstream"
  #fi
  #if [ ! -d "${srcdir}/${_realname}-${pkgver}.orig" ]; then
  #  cp -rp  "${srcdir}/${_realname}-${pkgver}" "${srcdir}/${_realname}-${pkgver}.orig"
  #fi

  cp -rp  "${srcdir}/${_realname}-${pkgver}/." "${srcdir}/build-${MINGW_CHOST}"
}

build() {
  cd "${srcdir}/build-${MINGW_CHOST}"

cat <<EOF >Make.user
USE_SYSTEM_SUITESPARSE = 1
# USE_SYSTEM_LIBUV = 1
# USE_SYSTEM_LIBM = 1
# USE_SYSTEM_DSFMT = 1
# USE_SYSTEM_LLVM = 1
USE_LLVM_SHLIB = 0
# USE_SYSTEM_PCRE = 1
USE_SYSTEM_BLAS = 1
LIBBLAS=-lopenblas
LIBBLASNAME=libopenblas
# USE_SYSTEM_GMP = 1
USE_SYSTEM_MBEDTLS = 1
USE_SYSTEM_LIBSSH2 = 1
USE_SYSTEM_LIBGIT2 = 1
# USE_SYSTEM_MPFR = 1
# USE_SYSTEM_UTF8PROC = 1
USE_SYSTEM_LAPACK = 1
LOCALBASE = ${MINGW_PREFIX}
prefix = ${MINGW_PREFIX}
EOF

  case "${CARCH}" in
    i686)
      export CFLAGS="-march=pentium4"
      export CXXFLAGS="-march=pentium4"
    ;;
  esac

  env CFLAGS="$CFLAGS -w" CXXFLAGS="$CXXFLAGS -w" make -j1
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"

  cd test
  ../usr/bin/julia --check-bounds=yes --startup-file=no ./runtests.jl all --skip Sockets --skip Distributed
  find ../stdlib \( -name \*.cov -o -name \*.mem \) -delete
  rm -r depot/compiled
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"

  make DESTDIR="${pkgdir}" install

  install -Dm644 LICENSE.md "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.md"
}
